package com.okbihuo.perm.mod.user.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.okbihuo.perm.core.auth.domain.BaseAuthUser;
import com.okbihuo.perm.core.mp.base.MyLogicModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.springframework.data.annotation.Transient;
import org.springframework.security.core.GrantedAuthority;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 认证用户类
 */
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class AuthUser extends MyLogicModel implements BaseAuthUser {

    /**
     * 唯一openId
     */
    @ApiModelProperty(value = "唯一openId")
    protected String openId;

    @ApiModelProperty(value = "用户名")
    protected String username;
    @ApiModelProperty(value = "密码")
    @JsonIgnore
    protected String password;
    @ApiModelProperty(value = "锁定标记")
    protected Integer locked;
    @ApiModelProperty(value = "超期时间")
    protected LocalDateTime expiresAt;
    @ApiModelProperty(value = "密码超期时间")
    protected LocalDateTime credentialsExpiresAt;

    @ApiModelProperty(value = "昵称")
    private String nickname;

    @ApiModelProperty(value = "用户类型")
    private String type;



    @ApiModelProperty(value = "创建客户端")
    private String registryClient;

    @ApiModelProperty(value = "注册时ip")
    private String registryIp;

    @ApiModelProperty(value = "最后一次登录IP")
    private String lastLoginIp;
    @ApiModelProperty(value = "最后一次登录时间")
    private String lastLoginTime;

    @ApiModelProperty(value = "手机号码")
    private String phone;

    @ApiModelProperty(value = "真实姓名")
    private String realName;

    @ApiModelProperty(value = "头像")
    private String avatar;
    @ApiModelProperty(value = "是否生效")
    protected Boolean enabled;

    /**
     * JSON格式记录的用户详情
     */
    @ApiModelProperty(value = "详情", example = "{QQ:123455667}")
    private String details;


    @ApiModelProperty(value = "角色列表")
    @TableField(exist = false)
    private List<AuthRole> roles;


    @ApiModelProperty(value = "权限列表")
    @TableField(exist = false)
    private List<AuthPermission> permissions;

    /**
     * Returns the authorities granted to the user. Cannot return <code>null</code>.
     *
     * @return the authorities, sorted by natural key (never <code>null</code>)
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return new ArrayList<>();
    }

    /**
     * Returns the password used to authenticate the user.
     *
     * @return the password
     */
    @Override
    public String getPassword() {
        return this.password;
    }

    /**
     * Returns the username used to authenticate the user. Cannot return <code>null</code>.
     *
     * @return the username (never <code>null</code>)
     */
    @Override
    public String getUsername() {
        return this.username;
    }

    /**
     * Indicates whether the user's account has expired. An expired account cannot be
     * authenticated.
     *
     * @return <code>true</code> if the user's account is valid (ie non-expired),
     * <code>false</code> if no longer valid (ie expired)
     */
    @Override
    public boolean isAccountNonExpired() {
        return expiresAt != null && expiresAt.isAfter(LocalDateTime.now());
    }

    /**
     * Indicates whether the user is locked or unlocked. A locked user cannot be
     * authenticated.
     *
     * @return <code>true</code> if the user is not locked, <code>false</code> otherwise
     */
    @Override
    public boolean isAccountNonLocked() {
        return locked == null || locked == 0;
    }

    /**
     * Indicates whether the user's credentials (password) has expired. Expired
     * credentials prevent authentication.
     *
     * @return <code>true</code> if the user's credentials are valid (ie non-expired),
     * <code>false</code> if no longer valid (ie expired)
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsExpiresAt != null && credentialsExpiresAt.isAfter(LocalDateTime.now());
    }

    /**
     * Indicates whether the user is enabled or disabled. A disabled user cannot be
     * authenticated.
     *
     * @return <code>true</code> if the user is enabled, <code>false</code> otherwise
     */
    @Transient
    @Override
    public boolean isEnabled() {
        return enabled;
    }

    /**
     * 返回用户确认用户唯一性的字符串，自动插入createdBy里面
     */
    @Override
    public String getAuditId() {
        return id.toString();
    }

    public String getOpenId() {
        return openId;
    }

    public Integer getLocked() {
        return locked;
    }

    public LocalDateTime getExpiresAt() {
        return expiresAt;
    }

    public LocalDateTime getCredentialsExpiresAt() {
        return credentialsExpiresAt;
    }

    public String getNickname() {
        return nickname;
    }

    public String getType() {
        return type;
    }

    public String getRegistryClient() {
        return registryClient;
    }

    public String getRegistryIp() {
        return registryIp;
    }

    public String getLastLoginIp() {
        return lastLoginIp;
    }

    public String getLastLoginTime() {
        return lastLoginTime;
    }

    public String getPhone() {
        return phone;
    }

    public String getRealName() {
        return realName;
    }

    public String getAvatar() {
        return avatar;
    }

    public String getDetails() {
        return details;
    }

    public List<AuthRole> getRoles() {
        return roles;
    }

    public List<AuthPermission> getPermissions() {
        return permissions;
    }
}
